package configuration;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import model.Console;
import model.Game;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

public class RomLoader {

    private static final Map<String,Console> mapConsoles = new HashMap<String,Console>();

    public static void load(){
        final Set<String> properties = PropertyUtils.getPropertyConsole();
        for(String property : properties)
        {
                System.out.println(property);
                final Map<String,String> attributes=PropertyUtils.getProperties(property);
                String extension ="rom";
                String romPath="";
                String emulatorPath ="";
                String ico="";

                for(String key:attributes.keySet()){
                    if(key.toLowerCase().contains("rom.extension")){
                        extension = attributes.get(key);
                    }
                    if(key.toLowerCase().contains("rom.path")){
                        romPath = attributes.get(key);
                    }
                    if(key.toLowerCase().contains("emulator.path")){
                        emulatorPath = attributes.get(key);
                    }
                    if(key.toLowerCase().contains("ico")){
                        ico = attributes.get(key);
                    }

                }
                final Console console = new Console(property,romPath,ico,extension,emulatorPath);
                final File directory = new File(romPath);
                final String ext = extension;
                final FilenameFilter filter = new FilenameFilter() {
                	 public boolean accept(File dir, String name) {
                         return name.endsWith(ext);
                     }
				};
                for(File file:directory.listFiles(filter)){
                    System.out.println(file);
                    final Game game = new Game(file.getName(),file.getAbsolutePath());
                    console.addGame(game);
                }
                mapConsoles.put(property,console);

        }
    }

    @SuppressWarnings("unchecked")
	public static final Collection<Console> getConsoles(final String filter) {
        final Predicate predicate = new Predicate() {
           
            public boolean evaluate(Object o) {
                final Console console = (Console) o;
                return console.getName().equals(filter);
            }
        };
        return CollectionUtils.select(mapConsoles.values(), predicate);
    }

    public static final Collection<Console> getConsoles(){
        return mapConsoles.values();
    }
    public static void printConsole(){
        for(Console console:mapConsoles.values()){
            System.out.println(console);
        }
    }

    public static void main(String[] args){
        RomLoader.load();
        RomLoader.printConsole();
    }
}
